package com.example.runner;

import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * java -jar my-spring-boot-app.jar --myOption --anotherOption=value1,value2 nonOptionArg1 nonOptionArg2
 */
@Component
public class CustomApplicationRunner implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(CustomApplicationRunner.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("Running CustomApplicationRunner with args: {}", JSONUtil.toJsonStr(args));

        // 检查是否包含特定选项
        if (args.containsOption("myOption")) {
            logger.info("Option myOption is present");
            // 执行对应的逻辑
        }

        // 获取指定选项的值
        List<String> anotherOptionValues = args.getOptionValues("anotherOption");
        if (anotherOptionValues != null) {
            for (String value : anotherOptionValues) {
                logger.info("Option anotherOption has value: {}", value);
                // 执行对应的逻辑
            }
        }

        // 获取所有非选项参数
        List<String> nonOptionArgs = args.getNonOptionArgs();
        for (String arg : nonOptionArgs) {
            logger.info("Non-option argument: {}", arg);
            // 执行对应的逻辑
        }
    }
}
